/*
 * FileAsMsg plug-in for Miranda IM and QIP Infium.
 * Sending/receiving files via instant messaging without protocol support.
 * Homepage: http://code.google.com/p/fileasmsg/
 *
 * Copyright (c) 2008, Radist <radist.nt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Name(s) of contributor(s) may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Radist ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Radist BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __FILEASMESSAGE_TRANSFER_FILE__H__R___
#define __FILEASMESSAGE_TRANSFER_FILE__H__R___

#include <Windows.h>
#include "coder.h"
#include "threads.h"
#include <vector>
#include <memory>

class TransferFile
{
	HANDLE hFile;
	HANDLE hMap;
	unsigned char *mptr;//pointer to mapped file part
	DWORD map_begin;//begin map address
	DWORD map_end;//end map address
	size_t chunk_begin;//mptr+chunk_begin - begin of the selected chunk
	static DWORD def_chunk_size;
	std::auto_ptr<CoderIf> coder;//for encoding, decoding and hashing
	std::vector<bool> completed;//bit mask of successfully downloaded chunks
	DWORD next_chunk;
	bool _read;
	CriticalSection cs;
	TransferFile(void);
	DWORD AdjustMap(DWORD chunk_no);//chunk_no is valid chunk number!!!
	DWORD ChunkSize(DWORD chunk_no)//chunk_no is valid chunk number!!!
	{
		if (chunk_no<file_chunks-1)
			return chunk_size;
		else 
		{
			DWORD last = file_size%chunk_size;
			return last?last:chunk_size;
		}
	}
	void RehashFile(void);//only for calling from CoderIf
	bool inline ReadRepeatChunkIndex(DWORD &index, const char *buf, char **next);
	friend class CoderIf;
public:
	wchar_t *name;
	DWORD file_size;
	DWORD chunk_size;
	DWORD file_chunks;
	bool keep_file;
	TransferFile(wchar_t *filename, DWORD flags, DWORD size = 0, DWORD _chunk_size = 0);//size=0 for reading
	~TransferFile(void);
	bool ReadData(char *buf, size_t buflen, DWORD chunk_no);
	bool WriteData(const char *buf, size_t buflen);
	bool GetRepeatList(char *buf, size_t buflen);//returns true if receiving is completed
	DWORD SetRepeatList(const char *buf);//returns true if receiving is completed

	bool GetNext(DWORD &next);
	bool CheckHash(void);
	DWORD inline chunks(void){return file_chunks;}

	static TransferFile *CreateTransferFile(wchar_t *filename, DWORD flags, DWORD file_size = 0, DWORD chunk_size = 0);
};

#define CHUNKNO_RADIX 36

#endif//__FILEASMESSAGE_TRANSFER_FILE__H__R___

